<template>
	<div>
		<div :class="['column', { active: active.includes(item.id) }]" v-for="item in list" :key="item.id">
			<div class="header" @click="toggleActive(item)">
				<el-checkbox
					:disabled="item.category != 1 && item.hasAuth"
					:value="+(value == item.id)"
					@change="onCheck($event, item)"
					:true-label="1"
					:false-label="0"
					class="el-checkbox"
				></el-checkbox>
				<span>{{ item.name }} ( {{ formatCategroy(item) }} )</span>
				<i class="el-icon-caret-right"></i>
			</div>
			<v-column-item :value="value" v-show="active.includes(item.id)" :list="item.children" @on-click="onClick"></v-column-item>
		</div>
	</div>
</template>

<script>
import { leftTreeAuthTree } from "@/api/cms/cmsarticle.js"
import VColumnItem from "./column-item.vue"

export default {
	name: "column",
	model: {
		prop: "value",
		event: "update:value",
	},
	props: {
		value: {},
		name: {},
	},
	data() {
		return {
			list: [],
			active: [],
		}
	},
	computed: {},
	async created() {
		const { code, data } = await leftTreeAuthTree()

		if (code == 0) {
			// this.active = data.map(item => item.id)
			this.list = data
		}
	},
	methods: {
		toggleActive(item) {
			if (this.active.includes(item.id)) {
				this.active.splice(this.active.indexOf(item.id), 1)
			} else {
				this.active.push(item.id)
			}
		},
		onClick({ node }) {
			this.$emit("update:value", node.id)
			this.$emit("update:name", node.name)
		},
		async onCheck(flag, node) {
			try {
				if (this.value) {
					await this.$confirm("该栏目可能有扩展字段, 切换可能会导致扩展字段数据丢失, 是否继续?", "提示", {
						confirmButtonText: "确定",
						cancelButtonText: "取消",
						type: "warning",
					})
				}
				this.onClick({ node: flag ? node : {} })
			} catch (error) {
				//
			}
		},
		formatCategroy(item) {
			return ["", "列表栏目", "封面栏目", "外链栏目"][item.category]
		},
	},
	components: {
		VColumnItem,
	},
}
</script>

<style lang="scss" scoped>
.column {
	&.active {
		.header {
			i {
				transform: rotate(90deg);
			}
		}
	}
	.header {
		background: #f5f7fa;
		color: #666666;
		border-bottom: 1px solid #ebeef5;
		line-height: 47px;
		padding-left: 10px;
		padding-right: 15px;
		display: flex;
		justify-content: space-between;
		align-items: center;
		cursor: pointer;

		.el-checkbox {
			margin-bottom: 0;
		}
		span {
			width: 90%;
		}
		i {
			transition: all 0.3s;
		}
		+ .column-item {
			margin-top: 5px;
			&:empty {
				margin-top: 0;
			}
		}
	}
}
</style>
